<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
获取系统配置值  C函数
*/
if (!function_exists('C')) {
	function C($param,$value='') {
		if ($value === '') {
			$val = model('SystenParamModel')->getSystemParam($param);
			return $val;
		} else {
			$val = model('SystenParamModel')->setSystemParam($param,$value);
			return $val;
		}
	}
}

/*
获取GET参数  G函数
*/
if (!function_exists('G')) {
	function G($param='') {
		if($param==''){
			$data = input('get.');
		} else {
			$data = input('get.'.$param);
		}
		return $data;	
	}
}


/*
获取POST参数  P函数
*/
if (!function_exists('P')) {
	function P($param='') {
		if($param==''){
			$data = input('post.');
		} else {
			$data = input('post.'.$param);
		}
		return $data;
	}
}


/*
响应函数R
*/
if (!function_exists('R')) {
	function R($code=0,$msg='success',$data='') {
		echo json_encode([
			'code'=>$code,
			'msg' =>$msg,
			'data'=>$data
		]);
		die();
	}
}

//16 to 10
if (!function_exists('decode_hex')) {
	function decode_hex($str) {
		if(substr($str, 0, 2) == '0x'){
			$str = substr($str, 2);
		}					
		if(preg_match('/[a-f0-9]+/', $str)){
			return hexdec($str);
		}		
		return $str;
	}
}
/*
curl
*/
if (!function_exists('curl')) {
	function curl($url,$post_data='',$type='get',$header=[]) {
		if ($post_data) {
			$type = 'post';
		}
		//初始化
	    $curl = curl_init();
	    //设置抓取的url
	    curl_setopt($curl, CURLOPT_URL, $url);
	    //设置头文件的信息作为数据流输出
	    curl_setopt($curl, CURLOPT_HEADER, 0);
	    //设置获取的信息以文件流的形式返回，而不是直接输出。
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    if($type=='post'){
	    	//设置post方式提交
    		curl_setopt($curl, CURLOPT_POST, 1);
    		if(is_array($post_data)){
    			$post_data = http_build_query($post_data);
    		}
    		curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
	    }
	    if($header){
	    	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
	    }
	    //https
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
       
	    //执行命令
	    $data = curl_exec($curl);
	    //关闭URL请求
	    curl_close($curl);
	    //显示获得的数据
	    return $data;
	}
}

function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./excel/', $isDown=false){  
    //include('Classes/PHPExcel.php');  
    // vendor('Classes.PHPExcel');
    require_once '../vendor/Classes/PHPExcel.php';
    $obj = new PHPExcel();  
    //横向单元格标识  
    $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
    $obj->getActiveSheet(0)->setTitle('sheet名称');   //设置sheet名称  
    $_row = 1;   //设置纵向单元格标识  
    if($title){  
        $_cnt = count($title);  
        $obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row);   //合并单元格  
        $obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '数据导出：'.date('Y-m-d H:i:s'));  //设置合并后的单元格内容  
        $_row++;  
        $i = 0;  
        foreach($title AS $v){   //设置列标题  
            $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);  
            $i++;  
        }  
        $_row++;  
    }  
    //填写数据  
    if($data){  
        $i = 0;  
        foreach($data AS $_v){  
            $j = 0;  
            foreach($_v AS $_cell){  
                $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);  
                $j++;  
            }  
            $i++;  
        }  
    }  
    //文件名处理  
    if(!$fileName){  
        $fileName = uniqid(time(),true);  
    }  
    $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');  
    if($isDown){   //网页下载  
        header('pragma:public');  
        header("Content-Disposition:attachment;filename=$fileName.xls");  
        $objWrite->save('php://output');exit;  
    }  
    $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码  
    $_savePath = $savePath.$_fileName.'.xlsx';  
    $objWrite->save($_savePath);  
    return $savePath.$fileName.'.xlsx';  

}